import * as api from '../api'
export default (state,action)=>{
    state=state || {users:[],setUser:[]}
    switch(action.type){
        case 'USERS':
            return {...state,users:action.users};
        case 'SETUSERS':
            return {...state,setUser:action.setUser}
        default:
            return state
    }
}
// 1将账号提供到数据库进行比对反馈结果
export const getUsers =(value)=>(dispatch)=>{
    fetch(api.USERS,{
        method:"post",
        headers:{"Content-Type":"application/json"},
        body:JSON.stringify({use:value})
    }).then(req=>req.json()).then(requlst=>{
        // console.log(requlst)
        dispatch({type:'USERS',users:requlst.data})
        })    
}
// 2将用户信息写入到数据库中
export const setUsers =(use,passwor)=>(dispatch)=>{
    fetch(api.SETUSER,{
        method:"post",
        headers:{"Content-Type":"application/json"},
        body:JSON.stringify({use,passwor})
    }).then(req=>req.json()).then(requlst=>{
        dispatch({type:'SETUSERS',setUser:requlst.data})
        })    
}